package com.edu.user.controller;

import com.edu.user.domain.dto.AppointmentRequestDTO;
import com.edu.user.domain.po.Appointment;
import com.edu.user.domain.po.User;
import com.edu.user.mapper.DoctorScheduleMapper;
import com.edu.user.service.AppointmentService;
import com.edu.user.utils.Result;
import com.edu.user.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/appointments")
public class AppointmentController {

    private final AppointmentService appointmentService;

    @Autowired
    public AppointmentController(AppointmentService appointmentService) {
        this.appointmentService = appointmentService;
    }

    @GetMapping("/patient")
    public Result getAppointmentsByPatient() {
        User user = ThreadLocalUtil.get();
        if (user == null) {
            log.warn("getAppointmentsByPatient: 未登录的用户尝试获取预约记录");
            return Result.error("请先登录");
        }

        log.info("getAppointmentsByPatient: 获取用户 {} 的预约记录", user.getId());
        return Result.success(appointmentService.getAppointmentsByPatient(user.getId()));
    }

    @PostMapping("/patient/make-appointment")
    public Result makeAppointment(@RequestParam Long schedulesId) {
        User user = ThreadLocalUtil.get();
        if (user == null) {
            log.warn("makeAppointment: 未登录的用户尝试预约");
            return Result.error("请先登录");
        }

        log.info("makeAppointment: 用户 {} 尝试预约时间表 {}", user.getId(), schedulesId);
        return appointmentService.makeAppointment(schedulesId);
    }

    @DeleteMapping("/patient/delete")
    public Result deleteAppointmentByPatient(@RequestParam Long appointmentId) {
        User user = ThreadLocalUtil.get();
        if (user == null) {
            log.warn("deleteAppointmentByPatient: 未登录的用户尝试删除预约记录");
            return Result.error("请先登录");
        }

        log.info("deleteAppointmentByPatient: 用户 {} 尝试删除预约记录 {}", user.getId(), appointmentId);
        boolean success = appointmentService.deleteAppointment(appointmentId);
        if (success) {
            log.info("deleteAppointmentByPatient: 用户 {} 成功删除预约记录 {}", user.getId(), appointmentId);
            return Result.success("删除预约记录成功");
        } else {
            log.error("deleteAppointmentByPatient: 用户 {} 删除预约记录 {} 失败", user.getId(), appointmentId);
            return Result.error("删除预约记录失败");
        }
    }

    @GetMapping("/doctor")
    public Result getAppointmentsByDoctor() {
        User user = ThreadLocalUtil.get();

        if (user == null) {
            log.warn("getAppointmentsByDoctor: 未登录的用户尝试获取医生  的预约记录");
            return Result.error("请先登录");
        }
        Long doctorId = user.getId();

        if (user.getId() != doctorId) {
            log.warn("getAppointmentsByDoctor: 用户 {} 尝试获取无权限的医生 {} 的预约记录", user.getId(), doctorId);
            return Result.error("权限不足");
        }

        log.info("getAppointmentsByDoctor: 医生 {} 获取自己的预约记录", doctorId);
        return Result.success(appointmentService.getAppointmentsByDoctor(doctorId));
    }

    @PostMapping("/doctor/confirm")
    public Result confirmAppointment(@RequestParam Long appointmentId) {
        User user = ThreadLocalUtil.get();
        if (user == null) {
            log.warn("confirmAppointment: 未登录的用户尝试确认预约 {}", appointmentId);
            return Result.error("请先登录");
        }

        log.info("confirmAppointment: 用户 {} 尝试确认预约 {}", user.getId(), appointmentId);
        return appointmentService.confirmAppointment(appointmentId);
    }

    @DeleteMapping("/doctor/delete")
    public Result deleteAppointmentByDoctor(@RequestParam Long appointmentId) {
        User user = ThreadLocalUtil.get();
        if (user == null) {
            log.warn("deleteAppointmentByDoctor: 未登录的用户尝试取消预约记录 {}", appointmentId);
            return Result.error("请先登录");
        }

        log.info("deleteAppointmentByDoctor: 用户 {} 尝试取消预约记录 {}", user.getId(), appointmentId);
        boolean success = appointmentService.deleteAppointmentByDoctor(appointmentId);
        if (success) {
            log.info("deleteAppointmentByDoctor: 用户 {} 成功取消预约记录 {}", user.getId(), appointmentId);
            return Result.success("取消预约记录成功");
        } else {
            log.error("deleteAppointmentByDoctor: 用户 {} 取消预约记录 {} 失败", user.getId(), appointmentId);
            return Result.error("取消预约记录失败");
        }
    }
}
